package com.lisn.coordinatorlib;


import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;

public class CoordinatorActivity extends AppCompatActivity {

    TabLayout tablayout;
    ViewPager viewpager;
    Toolbar toolbar;
    ImageView ivBg;
    AppBarLayout appbar;
    ImageView ivUseravator;
    TextView tv1;
    TextView tv2;


    private String[] titles = new String[]{"热门", "最新"};
    private ArrayList<Fragment> fragments = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_coordinator);
        tablayout = findViewById(R.id.tablayout);
        viewpager = findViewById(R.id.viewpager);
        toolbar = findViewById(R.id.toolbar);
        ivBg = findViewById(R.id.iv_bg);
        appbar = findViewById(R.id.appbar);
        ivUseravator = findViewById(R.id.iv_useravator);
        tv1 = findViewById(R.id.tv1);
        tv2 = findViewById(R.id.tv2);
        init();
        setAvatorChange();
    }

    private void init() {
        for (int i = 0; i < titles.length; i++) {
            fragments.add(new PartClassifyFragment());
            tablayout.addTab(tablayout.newTab());
        }

        viewpager.setAdapter(new FmPagerAdapter(fragments, getSupportFragmentManager()));
        tablayout.setupWithViewPager(viewpager);

        for (int j = 0; j < titles.length; j++) {
            tablayout.getTabAt(j).setText(titles[j]);
        }

        /**
         * 高斯模糊
         */
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.internet_star);
        ivBg.setImageBitmap(BlurImageView.BlurImages(bitmap));
    }

    /**
     * 渐变toolbar背景
     */
    private void setAvatorChange() {
        appbar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                //verticalOffset始终为0以下的负数
                float percent = (Math.abs(verticalOffset * 1.0f) / appBarLayout.getTotalScrollRange());

                toolbar.setBackgroundColor(changeAlpha(Color.WHITE, percent));
            }
        });
    }

    /**
     * 根据百分比改变颜色透明度
     */
    public int changeAlpha(int color, float fraction) {
        int red = Color.red(color);
        int green = Color.green(color);
        int blue = Color.blue(color);
        int alpha = (int) (Color.alpha(color) * fraction);
        return Color.argb(alpha, red, green, blue);
    }

    public static void startActivity(Context context) {
        context.startActivity(new Intent(context, CoordinatorActivity.class));
    }
}